package com.zyf.wjy.controller;

import com.zyf.wjy.common.Result;
import com.zyf.wjy.entity.News;
import com.zyf.wjy.service.NewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 新闻公告控制器
 */
@Controller
@RequestMapping("/news")
public class NewsController {
    
    @Autowired
    private NewsService newsService;
    
    /**
     * 新闻列表页面（后台管理）
     */
    @GetMapping("/list")
    public String list(Model model) {
        List<News> newsList = newsService.findAll();
        model.addAttribute("newsList", newsList);
        return "news/list";
    }
    
    /**
     * 新闻列表页面（前台展示）
     */
    @GetMapping("/index")
    public String index(Model model) {
        List<News> newsList = newsService.findPublished();
        model.addAttribute("newsList", newsList);
        return "news/index";
    }
    
    /**
     * 新闻详情页面
     */
    @GetMapping("/view/{id}")
    public String detail(@PathVariable Long id, Model model) {
        News news = newsService.findById(id);
        if (news != null) {
            // 增加浏览次数
            newsService.increaseViewCount(id);
            model.addAttribute("news", news);
        }
        return "news/detail";
    }
    
    /**
     * 获取新闻详情（API）
     */
    @GetMapping("/detail/{id}")
    @ResponseBody
    public Result<News> getDetail(@PathVariable Long id) {
        News news = newsService.findById(id);
        if (news != null) {
            return Result.success(news);
        } else {
            return Result.error("新闻不存在");
        }
    }
    
    /**
     * 新增/编辑页面
     */
    @GetMapping("/edit")
    public String edit(@RequestParam(required = false) Long id, Model model) {
        if (id != null) {
            News news = newsService.findById(id);
            model.addAttribute("news", news);
        }
        return "news/edit";
    }
    
    /**
     * 保存新闻
     */
    @PostMapping("/save")
    @ResponseBody
    public Result<News> save(@RequestBody News news) {
        try {
            News saved = newsService.save(news);
            return Result.success("保存成功", saved);
        } catch (Exception e) {
            return Result.error("保存失败：" + e.getMessage());
        }
    }
    
    /**
     * 发布新闻
     */
    @PostMapping("/publish/{id}")
    @ResponseBody
    public Result<Void> publish(@PathVariable Long id) {
        try {
            newsService.publish(id);
            return Result.success("发布成功", null);
        } catch (Exception e) {
            return Result.error("发布失败：" + e.getMessage());
        }
    }
    
    /**
     * 删除新闻
     */
    @PostMapping("/delete/{id}")
    @ResponseBody
    public Result<Void> delete(@PathVariable Long id) {
        try {
            boolean success = newsService.deleteById(id);
            if (success) {
                return Result.success("删除成功", null);
            } else {
                return Result.error("删除失败");
            }
        } catch (Exception e) {
            return Result.error("删除失败：" + e.getMessage());
        }
    }
}

